// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

AndroidAppItem {
    id: _root
    color: "#000000"

    property string dialBackgroundPath: ""

    ListModel{
        id: _model
        ListElement{ _img: "images/ComAndroidPhone/dial_num_1_no_vm_wht.png"; _num: "1"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_2_wht.png"; _num: "2"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_3_wht.png"; _num: "3"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_4_wht.png"; _num: "4"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_5_wht.png"; _num: "5"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_6_wht.png"; _num: "6"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_7_wht.png"; _num: "7"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_8_wht.png"; _num: "8"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_9_wht.png"; _num: "9"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_star_wht.png"; _num: "*"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_0_no_plus_wht.png"; _num: "0"}
        ListElement{ _img: "images/ComAndroidPhone/dial_num_pound_wht.png"; _num: "#"}
    }

    Image{
        width: 720 * viewScale
        height: 1134 * viewScale
        smooth: true
        source: "images/ComAndroidPhone/dial_background.png"
    }

    //テーマの背景
    Image{
        anchors.fill: _panel
        source: dialBackgroundPath
        fillMode: Image.PreserveAspectCrop
        smooth: true
        visible: status === Image.Ready
    }

    Column{
        id: _panel
        width: _root.width
        spacing: 4 * viewScale
        //タブ
        Image{
            width: 720 * viewScale
            height: 96 * viewScale
            smooth: true
            source: "images/ComAndroidPhone/dial_tab.png"
        }
        //入力エリア
        Image{
            width: 720 * viewScale
            height: 205 * viewScale
            source: "images/ComAndroidPhone/dial_background_texture.png"
            fillMode: Image.Tile
            smooth: true
            sourceSize.width: 7 * viewScale
            sourceSize.height: 7 * viewScale

            Row{
                anchors.fill: parent
                //入力数字
                Item{
                    width: parent.width - _backBt.width
                    height: parent.height
                    clip: true
                    Grid{
                        id: _inputNumbersArea
                        rows: 1
                        columns: 15
                        anchors.centerIn: parent
                        Repeater{
                            id: _inputNumbers
                            property int minusCount: 0
                            property bool isIncludeSymbol: false
                            model: ListModel{}
                            Image{
                                width: 38 * viewScale
                                height: 54 * viewScale
                                smooth: true
                                source: _img
                            }
                        }

                        //改行のコントロール
                        function updateLineCount(){
                            if(_inputNumbers.model.count % 15 === 0){
                                _inputNumbersArea.rows = Math.floor(_inputNumbers.model.count / 15) + 1;
                            }
                        }
                    }
                }
                //消すボタン
                Rectangle{
                    id: _backBt
                    x: 614 * viewScale
                    width: 106 * viewScale
                    height: parent.height
                    color: _mouseBackBt.pressed ? "#5539caff" : "#00000000"
                    Image{
                        x: 16 * viewScale
                        anchors.verticalCenter: parent.verticalCenter
                        width: 64 * viewScale
                        height: 64 * viewScale
                        smooth: true
                        source: "images/ComAndroidPhone/ic_dial_action_delete.png"
                    }
                    MouseArea{
                        id: _mouseBackBt
                        anchors.fill: parent
                        onClicked: {
                            if(_inputNumbers.model.count > 0){
                                var current = _inputNumbers.model.get(_inputNumbers.model.count-1)._num;
                                //消す
                                _inputNumbers.model.remove(_inputNumbers.model.count - 1);

                                //追加処理
                                if(_inputNumbers.model.count > 0){
                                    var i=0;
                                    var s = 0;
                                    //ハイフンの次を消したらもうひとつ
                                    if(_inputNumbers.model.get(_inputNumbers.model.count-1)._num === "-"){
                                        _inputNumbers.model.remove(_inputNumbers.model.count - 1);
                                    }
                                    //記号の削除をチェック
                                    if(current === "*" || current === "#"){
                                        //記号をチェック
                                        for(i=0; i<_inputNumbers.model.count; i++){
                                            if(_inputNumbers.model.get(i)._num === "*"
                                                    || _inputNumbers.model.get(i)._num === "#"){
                                                s += 1;
                                            }
                                        }
                                        if(s > 0){
                                            _inputNumbers.isIncludeSymbol = true;
                                        }else{
                                            _inputNumbers.isIncludeSymbol = false;
                                            //ハイフンを追加する
                                            if(_inputNumbers.model.count > 5){
                                                _inputNumbers.model.insert(5,{"_img": "images/ComAndroidPhone/dial_input_num_minus.png", "_num": "-"});
                                            }
                                            _inputNumbers.model.insert(3, {"_img": "images/ComAndroidPhone/dial_input_num_minus.png", "_num": "-"});
                                        }
                                    }
                                    //ハイフンが全く無ければ追加する
                                    if(_inputNumbers.model.count === 9 && _inputNumbers.isIncludeSymbol === false){
                                        for(i=0; i<_inputNumbers.model.count; i++){
                                            if(_inputNumbers.model.get(i)._num === "-"){
                                                s += 1;
                                            }
                                        }
                                        if(s > 0){
                                        }else{
                                            _inputNumbers.model.insert(5,{"_img": "images/ComAndroidPhone/dial_input_num_minus.png", "_num": "-"});
                                            _inputNumbers.model.insert(3, {"_img": "images/ComAndroidPhone/dial_input_num_minus.png", "_num": "-"});
                                        }
                                    }
                                }

                                //改行のコントロール
                                _inputNumbersArea.updateLineCount();
                            }
                        }
                    }
                }
            }
        }
        //テンキー
        Image{
            width: 720 * viewScale
            height: 668 * viewScale
            source: "images/ComAndroidPhone/dial_background_texture.png"
            fillMode: Image.Tile
            smooth: true
            sourceSize.width: 7 * viewScale
            sourceSize.height: 7 * viewScale
            Grid{
                anchors.horizontalCenter: parent.horizontalCenter
                rows: 4
                columns: 3
                Repeater{
                    model: _model
                    Rectangle{
                        width: 233 * viewScale
                        height: 162 * viewScale
                        color: _mouse.pressed ? "#5539caff" : "#00000000"
                        Image{
                            anchors.centerIn: parent
                            width: 180 * viewScale
                            height: 125 * viewScale
                            smooth: true
                            source: _img
                        }
                        MouseArea{
                            id: _mouse
                            anchors.fill: parent
                            function clearMinus(){
                                for(var i=_inputNumbers.model.count-1; i>=0; i--){
                                    if(_inputNumbers.model.get(i)._num === "-"){
                                        _inputNumbers.model.remove(i);
                                    }
                                }
                            }
                            onClicked: {
                                if(_inputNumbers.isIncludeSymbol === true){
                                    //記号が入ったので何もしない
                                }else if(_inputNumbers.model.count === 3){
                                    //3文字入力したのでハイフン追加
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_minus.png", "_num": "-"});
                                }else if(_inputNumbers.model.count === 6){
                                    //5文字入力したのでハイフン追加
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_minus.png", "_num": "-"});
                                }else if(_inputNumbers.model.count === 11){
                                    //9文字入力したのでハイフン削除
                                    clearMinus();
                                }

                                switch(_num){
                                case "0":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_0.png", "_num": "0"});
                                    break;
                                case "1":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_1.png", "_num": "1"});
                                    break;
                                case "2":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_2.png", "_num": "2"});
                                    break;
                                case "3":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_3.png", "_num": "3"});
                                    break;
                                case "4":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_4.png", "_num": "4"});
                                    break;
                                case "5":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_5.png", "_num": "5"});
                                    break;
                                case "6":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_6.png", "_num": "6"});
                                    break;
                                case "7":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_7.png", "_num": "7"});
                                    break;
                                case "8":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_8.png", "_num": "8"});
                                    break;
                                case "9":
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_9.png", "_num": "9"});
                                    break;
                                case "*":
                                    clearMinus();
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_aster.png", "_num": "*"});
                                    _inputNumbers.isIncludeSymbol = true;
                                    break;
                                case "#":
                                    clearMinus();
                                    _inputNumbers.model.append({"_img": "images/ComAndroidPhone/dial_input_num_sharp.png", "_num": "#"});
                                    _inputNumbers.isIncludeSymbol = true;
                                    break;
                                default:
                                    break;
                                }

                                //改行のコントロール
                                _inputNumbersArea.updateLineCount();
                            }
                        }
                    }
                }
            }
        }
        //メニューとか
        Image{
            width: 720 * viewScale
            height: 154 * viewScale
            source: "images/ComAndroidPhone/dial_background_texture.png"
            fillMode: Image.Tile
            smooth: true
            sourceSize.width: 7 * viewScale
            sourceSize.height: 7 * viewScale
            //検索
            Image{
                x: 28 * viewScale
                y: 76 * viewScale
                width: 64 * viewScale
                height: 64 * viewScale
                smooth: true
                source: "images/ComAndroidPhone/ic_dial_action_search.png"
            }
            //ダイヤル
            Image{
                anchors.horizontalCenter: parent.horizontalCenter
                y: 30 * viewScale
                width: 90 * viewScale
                height: 90 * viewScale
                smooth: true
                source: "images/ComAndroidPhone/ic_dial_action_call.png"
            }
            //メニュー
            Image{
                x: 628 * viewScale
                y: 76 * viewScale
                width: 64 * viewScale
                height: 64 * viewScale
                smooth: true
                source: "images/ComAndroidPhone/ic_dial_menu.png"
            }
        }
    }

}
